//
//  TopUpViewController.m
//  BMHVideo
//
//  Created by mac on 16/2/25.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "TopUpViewController.h"
#import "MoneyButton.h"
#import "HelpTool.h"
#import "NSString+Ext.h"
//#import "UIBarButtonItem+Ext.h"
#import "GlobTool.h"
#import "WeixinHelper.h"
#import <ReactiveCocoa.h>
#import <Masonry.h>
#import <StoreKit/StoreKit.h>   //添加内购系统框架
#import "BMHStoreObserver.h"    //内购single
#import "XLUserInfoModel.h"
#import "XLAccountManager.h"
#import "XLTopUpModel.h"
#import "XLHTTPSessionManager+UserInfo.h"
#import "UIImage+Gradient.h"


@interface TopUpViewController ()<UITextFieldDelegate>
@property (nonatomic,strong ) NSMutableArray  *moneyArray;
@property (nonatomic, strong) UIView          *footView;
@property (nonatomic, strong) UIView          *headerView;
@property (nonatomic, copy  ) NSString        *numberMoney;
@property (nonatomic, strong) UIButton        *moneyBtn;
@property (nonatomic, strong) UILabel         *detaleLabel;
@property (nonatomic, strong) UITextField     *textFiled;
@property (nonatomic, strong) UIButton        *shuruBtn;
@property (nonatomic, copy  ) NSString        *money;//支付要传的钱数
@property (nonatomic, copy  ) UIScrollView    *scrollView;
@property (nonatomic, copy  ) NSString        *beautyid;//美女的id
@property (nonatomic, copy  ) NSString        *convertRatioStr;
@property (nonatomic, copy  ) NSString        *payName;
@property (nonatomic, copy  ) NSString        *actionType;
@property (nonatomic, copy  ) NSString        *payorder;//支付宝订单号
@property (nonatomic, copy  ) NSString        *thirdPartyOrde;//KK订单号
@property (nonatomic, strong) NSDictionary    *zfbDic;
@property (nonatomic, strong) NSString        *productIds;//是在开发者平台填写的产品ID
@property (nonatomic, strong) XLUserInfoModel *userInfoModel;
@property (nonatomic, strong) NSMutableArray     *topUpMoneyArr;
@property (nonatomic, strong) NSMutableArray     *topUpJDArr;
@property (nonatomic, strong) UILabel            *topUpJDNumber;
@property (nonatomic, strong) UIView             *xlPayView;
@property (nonatomic, strong) UIView             *xlWXPayView;
@property (nonatomic, strong) UIView             *xlZFBPayView;
@property (nonatomic, strong) UIButton           *xlWXSeletBtn;
@property (nonatomic, strong) UIButton           *xlZFBSeletBtn;
@property (nonatomic, strong) NSString           *xlSelecType;
@property (nonatomic, assign) NSInteger           seleHeight;



//沙盒测试环境验证
#define SANDBOX  @"https://sandbox.itunes.apple.com/verifyReceipt"
//正式环境验证
#define AppStore @"https://buy.itunes.apple.com/verifyReceipt"
//价格*0.7
#define AppStorePrice 0.7


@end

@implementation TopUpViewController
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (id)initWithBeautyid:(NSString *)beautyid {
    if (self = [super init]) {
  
        self.beautyid = beautyid;
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(paySuccess) name:BMHRefreshUserInfo object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payFail) name:@"zhifushibai" object:nil];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BMHRefreshUserInfo object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"zhifushibai" object:nil];
    
}

- (void)paySuccess {
    if (self.backActionBlock) {
        self.backActionBlock();
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)payFail {
    if (self.backActionBlock) {
        self.backActionBlock();
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
//    self.navigationController.navigationBarHidden = YES;
    
   
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
//    [BMHStatisticsTool endLogPageView:@"livecharge"];
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    [_scrollView scrollsToTop];
//    [BMHStatisticsTool beginLogPageView:@"livecharge"];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.xlSelecType = @"0";
    self.zfbDic = [NSDictionary dictionary];
    self.topUpMoneyArr = [NSMutableArray array];
    self.topUpJDArr    = [NSMutableArray array];
    self.userInfoModel = [[XLAccountManager shareInstance] model];
    UILabel * customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    customLab.backgroundColor = kClearColor;
    customLab.font = kSetFont(18);
    customLab.textColor = kBlackColor;
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.text = @"充值中心";
    self.navigationItem.titleView = customLab;
    self.navigationItem.titleView.userInteractionEnabled = YES;
    [self tapGestureRecognizer:self.navigationItem.titleView];
    self.view.backgroundColor = XLBgColor;
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,70, 22)];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [rightBtn setTitle:@"充值记录" forState:UIControlStateNormal];
    rightBtn.hidden = YES;/////////
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(finishActionbar) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    if (self.backActionBlock) {
        [self showBackButton:self.backActionBlock];
    } else {
        [self showBackButton:NULL];
    }
    [self GetMoneyList];
    if ([[UserDefaults objectForKey:XiangLove_OpenApplePay] boolValue] == YES) {
        self.moneyArray = [NSMutableArray arrayWithObjects:@"6", @"30", @"50", @"108", @"208", @"518", nil];
    }else{
        self.moneyArray = [UserDefaults objectForKey:@"oldVC"];
    }
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap1.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap1];
    if (self.imPays) {
        
        self.convertRatioStr = self.imPays;
        self.payName  = @"秀币";
    }else{
        self.convertRatioStr = ConvertRatio;
        self.payName  = @"个金豆";
        
    }
    
    [self addObserver];
    
}

- (void)addObserver {
    @weakify(self);
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"zhifusuccess" object:nil] subscribeNext:^(id x) {
        @strongify(self);
        [self refreshUserInfo];
    }];
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:BMHRefreshUserInfo object:nil] subscribeNext:^(id x) {
        @strongify(self);
        [self refreshUserInfo];
    }];
}

- (void)refreshUserInfo {
    [[[XLHTTPSessionManager sharedManager] getUserInfoWithUserId:[[XLAccountManager shareInstance] model].Id secretKey:[[XLAccountManager shareInstance] model].SecretKey] subscribeNext:^(id x) {
        self.userInfoModel = x;
        [UserDefaults setObject:self.userInfoModel.Id forKey:UserID];
        [UserDefaults setObject:self.userInfoModel.HeadImg forKey:UserHead];
        [UserDefaults setObject:self.userInfoModel.NickName forKey:UserNick];
        self.userInfoModel.SecretKey = [[XLAccountManager shareInstance] model].SecretKey;
        [[XLAccountManager shareInstance] updateUserInfo:self.userInfoModel];
        [self setUpdate];
    } error:^(NSError *error) {
        
    }];
}

//创建轻拍手势
-(void)tapGestureRecognizer:(UIView *)view
{
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self     action:@selector(tapAction:)];
    tap.numberOfTapsRequired    =3;
    tap.numberOfTouchesRequired =1;
    [view addGestureRecognizer:tap];
}
//轻拍事件
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    [self testTopupMoney];
    
}
/**
 预留：充值记录
 */
-(void)finishActionbar
{
    BMHLog(@"充值记录");
}
-(void)GetMoneyList
{
    //[UserDefaults setObject:@(YES) forKey:XLIsTest];
    [BMHHTTPSession GETBMHVideo:@"/api/v1/VideoChatInfo/GetRechareInfo" paraments:@{@"type":@"2"} success:^(id  _Nullable JSON) {
        
        NSLog(@"新用户列表:%@",JSON);
        [self getTopupList:JSON[@"data"]];
    } failure:^(NSError * _Nullable error) {
        NSLog(@"%@", error);
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showContent:@"加载失败" toView:self.view];
    }];
    
}

-(void)getTopupList:(NSMutableArray *)arr
{
    for (int i = 0; i < arr.count; i++) {
        
        XLTopUpModel * topupMd = [XLTopUpModel modelWithDictionary:arr[i]];
        [self.topUpMoneyArr addObject:topupMd.money];
        [self.topUpJDArr    addObject:topupMd.jindou];

    }
    
    self.moneyArray = self.topUpMoneyArr;
    [UserDefaults setObject:self.moneyArray forKey:@"oldVC"];
    [self setupData];
   
    
}

-(void)setupData
{
    if ([[UserDefaults objectForKey:XiangLove_OpenApplePay] boolValue]  == YES) {
        
        self.moneyArray = [NSMutableArray arrayWithObjects:@"6", @"30", @"50", @"108", @"208", @"518", nil];
        
    }else{
    
        self.moneyArray = self.topUpMoneyArr;
        if ([_LiveCharge isEqualToString:@"YES"]) {
            self.moneyArray = [NSMutableArray arrayWithObjects:@"6", @"30", @"100", @"300", @"500",@"1000",@"5000",@"10000", nil];
        }
    }
    [self AddFootViewAndHeaderView];
    
}


#pragma mark- SKRequestDelegate

-(void)requestDidFinish:(SKRequest*)request
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}
-(void)request:(SKRequest*)requestdidFail WithError:(NSError*)error
{
    
    [MBProgressHUD showContent:@"支付失败"];
}

- (void)showBackButton:(void (^)())clickBlock{
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    [backButton setImageEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)];
    
    UIImage *backImg = [UIImage imageNamed:@"common_nav_btn_back_n"];
    [backButton setImage:backImg forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    
    @weakify(self);
    [[backButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        if (clickBlock) {
            clickBlock();
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

-(void)back
{
    if (self.navigationController) {
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }
    
    if (self.presentingViewController) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
}
- (void)handleTopLeftback {
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewTapped:(UITapGestureRecognizer*)tap1 {
    
    [self.view endEditing:YES];
}
-(void)setUpdate
{
    _topUpJDNumber.text = [NSString stringWithFormat:@"%@",self.userInfoModel.AccountBalanceString];
}
- (void)AddFootViewAndHeaderView {
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64)];
    scrollView.backgroundColor = XLBgColor;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [scrollView scrollsToTop];
    _scrollView = scrollView;
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 120)];
    headerView.backgroundColor = XLThemeColor;
    [scrollView addSubview:headerView];
    _headerView = headerView;
    _topUpJDNumber = [[UILabel alloc] init];
    _topUpJDNumber.textAlignment = NSTextAlignmentCenter;
    _topUpJDNumber.userInteractionEnabled = YES;
    _topUpJDNumber.font = [UIFont systemFontOfSize:40];
    _topUpJDNumber.textColor = HEXCOLOR(0x000000);
    _topUpJDNumber.text = [NSString stringWithFormat:@"%@",self.userInfoModel.AccountBalanceString];
    [_headerView addSubview:_topUpJDNumber];
    [_topUpJDNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_headerView);
        make.top.equalTo(_headerView.mas_top).offset(18);
    }];
    
    //添加充值彩蛋
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapReChargeMoney:)];
    tapGesture.numberOfTapsRequired = 2;
    tapGesture.numberOfTouchesRequired = 1;
    [self.topUpJDNumber addGestureRecognizer:tapGesture];
    
    UIView * xlLine = [[UIView alloc] init];
    xlLine.backgroundColor = XLLineColor;
    [_headerView addSubview:xlLine];
    [xlLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headerView.mas_bottom).offset(-1);
        make.size.mas_equalTo(CGSizeMake(XQScreenW, 1));
    }];
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:13];
    label.textColor = kDarkGrayColor;
    label.text = @"金豆余额(个)";
    [_headerView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_headerView);
        make.top.equalTo(_topUpJDNumber.mas_bottom);
    }];
    
    if ([[UserDefaults objectForKey:XiangLove_OpenApplePay] boolValue]  == YES){
        self.seleHeight = 130;
    }else{
        self.seleHeight = 168;
        if ([_LiveCharge isEqualToString:@"YES"]) {
             self.seleHeight = 44;
        }
        UIView * payView = [[UIView alloc] initWithFrame:CGRectMake(0, 120, [UIScreen mainScreen].bounds.size.width, 44)];
        if ([_LiveCharge isEqualToString:@"YES"]) {
            payView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
        }
        payView.backgroundColor = XLBgColor;
        [_scrollView addSubview:payView];
        _xlPayView = payView;
        
        //微信支付
        UIView * xlWXPayView = [[UIView alloc] init];
        xlWXPayView.backgroundColor = XLThemeColor;
        [_xlPayView addSubview:xlWXPayView];
        [xlWXPayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_xlPayView.mas_top);
            make.size.mas_equalTo(CGSizeMake(XQScreenW, 44));
        }];
        _xlWXPayView = xlWXPayView;
        
        UIImageView * xlWXImgView = [[UIImageView alloc] init];
        xlWXImgView.image = [UIImage imageNamed:@"common_icon_weixin_n"];
        [_xlWXPayView addSubview:xlWXImgView];
        [xlWXImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_xlWXPayView);
            make.left.equalTo(_xlWXPayView.mas_left).offset(15);
            make.size.mas_equalTo(CGSizeMake(29, 29));
        }];
        
        UILabel * xlWXTitle = [[UILabel alloc] init];
        xlWXTitle.text = @"微信支付";
        xlWXTitle.font = kSetFont(17);
        xlWXTitle.textColor = kBlackColor;
        [_xlWXPayView addSubview:xlWXTitle];
        [xlWXTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_xlWXPayView);
            make.left.equalTo(xlWXImgView.mas_right).offset(14);
        }];
        
        UIButton * xlWXSeletBtn = [[UIButton alloc] init];
        [xlWXSeletBtn setImage:[UIImage imageNamed:@"common_but_gouxuan_n"] forState:UIControlStateNormal];
        [xlWXSeletBtn setImage:[UIImage imageNamed:@"common_but_gouxuan_s"] forState:UIControlStateSelected];
        xlWXSeletBtn.selected = YES;
        [xlWXSeletBtn addTarget:self action:@selector(xlwxSeleAction:) forControlEvents:UIControlEventTouchUpInside];
        [_xlWXPayView addSubview:xlWXSeletBtn];
        [xlWXSeletBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_xlWXPayView);
            make.right.equalTo(_xlWXPayView.mas_right).offset(-12);
            make.size.mas_equalTo(CGSizeMake(26, 26));
        }];
        _xlWXSeletBtn = xlWXSeletBtn;
        
        
        UIView * xlWXLine = [[UIView alloc] init];
        xlWXLine.backgroundColor = XLLineColor;
        [_xlWXPayView addSubview:xlWXLine];
        [xlWXLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_xlWXPayView.mas_bottom).offset(-1);
            make.left.equalTo(_xlWXPayView.mas_left).offset(15);
            make.size.mas_equalTo(CGSizeMake(XQScreenW-15, 1));
        }];
        
        //支付宝支付
        UIView * xlZFBPayView = [[UIView alloc] init];
        xlZFBPayView.backgroundColor = XLThemeColor;
        [_xlPayView addSubview:xlZFBPayView];
        [xlZFBPayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_xlWXPayView.mas_bottom);
            make.size.mas_equalTo(CGSizeMake(XQScreenW, 0));
        }];
        _xlZFBPayView = xlZFBPayView;
        _xlZFBPayView.hidden = YES;
        UIImageView * xlZFBImgView = [[UIImageView alloc] init];
        xlZFBImgView.image = [UIImage imageNamed:@"common_icon_zfb_n"];
        [_xlZFBPayView addSubview:xlZFBImgView];
        [xlZFBImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_xlZFBPayView);
            make.left.equalTo(_xlZFBPayView.mas_left).offset(15);
            make.size.mas_equalTo(CGSizeMake(29, 29));
        }];
        
        UILabel * xlZFBTitle = [[UILabel alloc] init];
        xlZFBTitle.text = @"支付宝支付";
        xlZFBTitle.font = kSetFont(17);
        xlZFBTitle.textColor = kBlackColor;
        [_xlZFBPayView addSubview:xlZFBTitle];
        [xlZFBTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_xlZFBPayView);
            make.left.equalTo(xlZFBImgView.mas_right).offset(14);
        }];
        
        UIButton * xlZFBSeletBtn = [[UIButton alloc] init];
        [xlZFBSeletBtn setImage:[UIImage imageNamed:@"common_but_gouxuan_n"] forState:UIControlStateNormal];
        [xlZFBSeletBtn setImage:[UIImage imageNamed:@"common_but_gouxuan_s"] forState:UIControlStateSelected];
        [xlZFBSeletBtn addTarget:self action:@selector(xlzfbSeletAcion:) forControlEvents:UIControlEventTouchUpInside];
        [_xlZFBPayView addSubview:xlZFBSeletBtn];
        [xlZFBSeletBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_xlZFBPayView);
            make.right.equalTo(_xlZFBPayView.mas_right).offset(-12);
            make.size.mas_equalTo(CGSizeMake(26, 26));
        }];
        _xlZFBSeletBtn = xlZFBSeletBtn;
        
        UIView * xlZFBLine = [[UIView alloc] init];
        xlZFBLine.backgroundColor = XLLineColor;
        [_xlZFBPayView addSubview:xlZFBLine];
        [xlZFBLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_xlZFBPayView.mas_bottom).offset(-1);
            make.size.mas_equalTo(CGSizeMake(XQScreenW, 1));
        }];
        
    }
    
    //选择金额
    
    for (int i = 0; i < self.moneyArray.count; i++) {
        
           NSString * moneyStr = self.moneyArray[i];
           NSString *tempStr;
           if ([[UserDefaults objectForKey:XiangLove_OpenApplePay] boolValue]  == YES) {
                tempStr = [NSString stringWithFormat:@"%.f%@",(long)[moneyStr integerValue]*[self.convertRatioStr integerValue]*AppStorePrice,self.payName];
            }else{
               tempStr = [NSString stringWithFormat:@"%ld%@",(long)[moneyStr integerValue]*[self.convertRatioStr integerValue],self.payName];
                if ([_LiveCharge isEqualToString:@"YES"]) {
                    tempStr = [NSString stringWithFormat:@"%ld%@",(long)[moneyStr integerValue]*1000,@"个秀币"];
                }
            }
            UIView * xselectedView = [[UIView alloc] initWithFrame:CGRectMake(0, self.seleHeight + 50 * i,XQScreenW,49)];
            xselectedView.backgroundColor = XLThemeColor;
            [scrollView addSubview:xselectedView];
            UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, self.seleHeight + 50 * i, 150, 49)];
            numberLabel.font = [UIFont systemFontOfSize:16];
            numberLabel.text = tempStr;
            numberLabel.textAlignment = NSTextAlignmentLeft;
            numberLabel.textColor = kBlackColor;
            [scrollView addSubview:numberLabel];
        
        
            MoneyButton *xuanBtn = [MoneyButton buttonWithType:UIButtonTypeCustom];
            xuanBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 89, self.seleHeight+10+50 * i,74, 28);
            xuanBtn.layer.masksToBounds = YES;
            xuanBtn.layer.cornerRadius = 14;
            xuanBtn.monry = self.moneyArray[i];
            [xuanBtn setBackgroundColor:RGB_HEX(0xffbb01)];
            [xuanBtn setTitle:[NSString stringWithFormat:@"¥ %@",self.moneyArray[i]] forState:UIControlStateNormal];
            xuanBtn.titleLabel.font = kSetFont(14);
            [xuanBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
            [xuanBtn addTarget:self action:@selector(handelXuanBtn:) forControlEvents:UIControlEventTouchUpInside];
            [scrollView addSubview:xuanBtn];
            
            UILabel *xianLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, self.seleHeight + 49 + 50 * i, [UIScreen mainScreen].bounds.size.width - 15, 1)];
            xianLabel.backgroundColor = XLLineColor;
            [scrollView addSubview:xianLabel];
    }
    [_scrollView scrollsToTop];
}

/**
 充值彩蛋
 */
- (void)tapReChargeMoney:(UITapGestureRecognizer *)sender
{
    [[WeixinHelper sharedWeixinHelper] getUrlWithWXMoney:@"0.1" Months:@"0" TranstypeId:@"1" RemarksInfo:RemarksRose KKOrder:@"" kkUserid:@""];
//    self.money = @"0.1";
//    [self hadenlTopUp:nil];
}

#pragma mark -
#pragma mark 选择微信支付
-(void)xlwxSeleAction:(UIButton *)sender
{
    self.xlSelecType        = @"0";
    _xlWXSeletBtn.selected  = YES;
    _xlZFBSeletBtn.selected = NO;
}

#pragma mark -
#pragma mark 选择支付宝支付
-(void)xlzfbSeletAcion:(UIButton *)sender
{
    self.xlSelecType        = @"1";
    _xlZFBSeletBtn.selected = YES;
    _xlWXSeletBtn.selected  = NO;
}
-(void)testTopupMoney
{
    self.money = @"1";
}
#pragma mark -
#pragma mark 开始支付(self.xlSelecType:0-微信支付,1-支付宝支付)
- (void)handelXuanBtn:(MoneyButton *)sender {
    self.moneyBtn = sender;
    if ([self.money isEqualToString:@"1"]) {
        self.money = @"1";
    }else{
       self.money = sender.monry;
    }
    
    if ([[UserDefaults objectForKey:XiangLove_OpenApplePay] boolValue]  == YES){
        [self hadenlTopUpIn_purchase:sender];
    }
    else
    {
       if ([self.xlSelecType isEqualToString:@"0"]) {
            [self hadelTopWeixin:self.moneyBtn];
        }else{
            [self hadenlTopUp:self.moneyBtn];
        }
    }
}
#pragma mark -
#pragma mark 苹果内购
-(void)payBtn:(UIButton *)sender
{
    [self hadenlTopUpIn_purchase:sender];
}
#pragma mark -
#pragma mark 微信充值
- (void)hadelTopWeixin:(UIButton *)sender {
    
    if (!([self.money integerValue] > 0)) {
        [HelpTool startTipViewWithText:@"请选择你需要的金额!" duration:1.5 type:kTipViewTypeNone];
    } else {
        self.actionType = @"1";
        if ([_LiveCharge isEqualToString:@"YES"]){
            self.actionType = @"5";
        }
        [[WeixinHelper sharedWeixinHelper] getUrlWithWXMoney:self.money Months:@"0" TranstypeId:self.actionType RemarksInfo:RemarksRose KKOrder:@"" kkUserid:@""];
        
    }
}

#pragma mark -
#pragma mark 支付宝充值
- (void)hadenlTopUp:(UIButton *)sender {
    
    if (!([self.money integerValue] > 0)) {
        [HelpTool startTipViewWithText:@"请选择你需要的金额!" duration:1.5 type:kTipViewTypeNone];
    } else {
        self.actionType = @"1";
        if ([_LiveCharge isEqualToString:@"YES"]){
            self.actionType = @"5";
        }
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [[[XLHTTPSessionManager sharedManager] getZFBPlayOrder:self.userInfoModel.Id giftId:@"1" TransType:self.actionType payType:@"4" FeeType:@"" Money:self.money PrivateInfo:zhf_PrivateKey Remarks:RemarksRose Months:@"0" secretKey:self.userInfoModel.SecretKey] subscribeNext:^(id x) {
            BMHLog(@"===支付宝创建订单===%@",x);
            NSString * success = [NSString stringWithFormat:@"%@",x[@"success"]];
            self.payorder      = [NSString stringWithFormat:@"%@",x[@"data"]];
            if ([success isEqualToString:@"0"]) {
                NSString *money = [NSString stringWithFormat:@"%.2f", [self.money floatValue]];
                
                if (self.imPays) {
//                    [ZhifuHelp InPutzhifubaoWithDingdan:self.payorder Money:money subjesct:RemarkKKPay body:RemarkKKPay successBlock:^{
//                        [self.navigationController popViewControllerAnimated:YES];
//                    } failBlock:^{
//                        [self.navigationController popViewControllerAnimated:YES];
//                    }];
//
                }else{
//                    
//                    [ZhifuHelp InPutzhifubaoWithDingdan:self.payorder Money:money subjesct:RemarksRose body:RemarksRose successBlock:^{
//                        [self.navigationController popViewControllerAnimated:YES];
//                    } failBlock:^{
//                        [self.navigationController popViewControllerAnimated:YES];
//                    }];
                    
                }
                
                
            } else {
                
                [HelpTool startTipViewWithText:@"创建订单失败" duration:1.0 type:kTipViewTypeNone];
            }
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            
        } error:^(NSError *error) {
            NSLog(@"错误:%@",error);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [MBProgressHUD showContent:@"加载失败,请重试!" toView:self.view];
            
        }];

    }
}

#pragma mark -
#pragma mark AppStore内购支付订单
- (void)hadenlTopUpIn_purchase:(UIButton *)sender {
    if (!([self.money integerValue] > 0)) {
        [HelpTool startTipViewWithText:@"请选择你需要的金额!" duration:1.5 type:kTipViewTypeNone];
    } else {
        
        
        self.actionType = @"1";
        self.actionType = @"1";
        if ([_LiveCharge isEqualToString:@"YES"]){
            self.actionType = @"5";
        }
        [MBProgressHUD showMessage:nil];
        NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:UserID];
        NSDate *now = [NSDate date];
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = kBMHRequestTimeFormat;
        //时间戳
        NSString *sTime = [fmt stringFromDate:now];
        NSString *objID = @"0";
        NSString *publicKey = @"BMH2014YDYWB";
        NSString *actionType = self.actionType;
        NSString *nat = [NSString stringWithFormat:@"%@%@%@%@%@",objID, uid, publicKey, actionType, sTime];
        nat = [nat md5Encrypt];
        NSDate * senddate=[NSDate date];
        NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"YYYY-MM-dd_HH:mm"];
        NSString *locationString = [dateformatter stringFromDate:senddate];

        self.zfbDic = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"Type",uid,@"Userid",@"1",@"Giftid",self.actionType,@"Transtype",@"6",@"Paytype",@"",@"Feetype",self.money,@"Money",zhf_PrivateKey,@"Privateinfo",RemarksRose,@"Remarks",@"0",@"Months",locationString,@"Timestamp",nat,@"Appsign",kAppTypeOrId,@"appid",KAPPFr,@"qudao",nil];

        [BMHHTTPSession GET:@"/MobileInterface/Pay/AliPay.ashx" paraments:self.zfbDic success:^(id  _Nullable JSON) {
            NSLog(@"===苹果内购创建订单===%@",JSON);
            NSString *errcode = JSON[@"result"][@"errcode"];
            self.payorder = JSON[@"result"][@"OrderNo"];
            if ([errcode isEqualToString:@"0"]) {
                self.productIds= [NSString stringWithFormat:@"xnjy%@",self.money];
                if([SKPaymentQueue canMakePayments])
                {
                    //buy AppStore Products
                    [[BMHStoreObserver sharedInstance] buy:self.productIds withPayMonyNum:self.money andPayOrder:self.payorder];

                }else
                {
                    BMHLog(@"不允许程序内付费");
                }

            } else {

                [HelpTool startTipViewWithText:@"创建订单失败" duration:1.0 type:kTipViewTypeNone];
            }

            [MBProgressHUD hideHUD];
        } failure:^(NSError * _Nullable error) {
            [MBProgressHUD hideHUD];
        }];
    }
}
#pragma mark -

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}


#pragma mark - 屏幕方向改变
- (BOOL)shouldAutorotate
{
    return NO;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
